package controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.TreeMap;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.html.HTMLDocument.Iterator;

import model.kwalificatiebeheer.Kerntaak;
import model.kwalificatiebeheer.KwalificatieDossier;
import model.kwalificatiebeheer.KwalificatieDossiers;
import view.AddObjectOptionPane;
import view.KerntaakFrame;
import view.KerntaakTableModel;
import view.KwalificatieDossierPanel;

public class KwalificatieDossierPanelActions implements ActionListener, MouseListener, Observer {
	
	private KwalificatieDossierPanel sender;
	
	public KwalificatieDossierPanelActions(KwalificatieDossierPanel sender){
		this.sender = sender;
		KwalificatieDossiers.getInstance().addObserver(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("btnLijst")){
			sender.initTable();
		}else if(e.getActionCommand().equals("btnNieuw")){
			sender.initForm("new");
		}else if(e.getActionCommand().equals("btnSaveNew")){
			saveFrom("new");
			sender.initTable();
		}else if(e.getActionCommand().equals("btnSaveEdit")){
			saveFrom("edit");
			sender.initTable();
		}else if(e.getActionCommand().equals("btnEdit")){
			sender.initForm("edit");
		}else if(e.getActionCommand().equals("btnCancel")){
			sender.initForm("display");
		}else if(e.getActionCommand().equals("btnDelete")){
			sender.deleteActive();
		}else if(e.getActionCommand().equals("removeKd")){
			KwalificatieDossier aKd = sender.getActive();
			KwalificatieDossiers.getInstance().deleteKwalificatieDossier(aKd);
		}else if(e.getActionCommand().equals("addKerntaak")){
			KwalificatieDossier aKd = sender.getActive();
			AddObjectOptionPane ap = new AddObjectOptionPane("Kerntaak");
			if(ap.getResults().size() > 0){
				Kerntaak newKt = new Kerntaak(aKd.getKerntaken().size()+1, aKd, ap.getResults().get("title"));
				aKd.addKerntaak(newKt);
			}
		}else if(e.getActionCommand().equals("removeKerntaak")){
			KwalificatieDossier aKd = sender.getActive();
			aKd.removeKerntaak(sender.getActiveKt());
		}
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getClickCount() == 1){
			int row = sender.getTable().getSelectedRow();
			KwalificatieDossier kd = KwalificatieDossiers.getInstance().getKwalificatieDossiers().get(row);
			sender.setActive(kd);
		}else if(e.getClickCount() == 2){
			int row = sender.getTable().getSelectedRow();
			KwalificatieDossier kd = KwalificatieDossiers.getInstance().getKwalificatieDossiers().get(row);
			sender.setActive(kd);
			sender.initForm("display");
		}
	}
	
	public KwalificatieDossier saveFrom(String mode){
		KwalificatieDossier active = sender.getActive();
		Long[] crebos;
		if(!(sender.getTextCreboField().getText().isEmpty())){
			String[] crebosString = sender.getTextCreboField().getText().replaceAll("[^0-9,{1}]","").split(",");
			ArrayList<Long> creboList = new ArrayList<Long>();
			for(int i=0; i < crebosString.length; i++){
				if(!(crebosString[i].isEmpty())){
					creboList.add(Long.parseLong(crebosString[i]));
				}
			}
			crebos = new Long[creboList.size()];
			int i = 0;
			for(Long crebo : creboList){
				crebos[i] = crebo;
				i++;
			}
		}else{
			crebos = new Long[]{};
		}
		if(mode == "new"){
			return new KwalificatieDossier(1, crebos, Long.parseLong(sender.getTextStartDateField().getText()), sender.getTextSectorField().getText(), sender.getTextDomainField().getText(), sender.getTextBranchField().getText());
		}else if(mode == "edit"){
			String start = sender.getTextStartDateField().getText().replaceAll("[^0-9]", "");
			active.update(crebos, Long.parseLong(start), sender.getTextSectorField().getText(), sender.getTextDomainField().getText(), sender.getTextBranchField().getText());
			return active;
		}
		return null;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
	@Override
	public void mouseReleased(MouseEvent e) {}

	@Override
	public void update(Observable arg0, Object arg1) {
				
	}
}
